<template>
  <view>
    <!-- 搜索框 -->
    <my-search bgcolor="#ff00ff" radius="8"  @click="searchClickHandler" ></my-search>
    
    <!-- 分类导航 -->
    <view class="cate-container">
      <!-- 左侧scroll -->
      <scroll-view class="cate-left" scroll-y="true" :style="{height: wh+'px'}">
        <view
          :class="['cate-left-item', i===active ? 'active': '']"
          v-for="(item, i) in cateList"
          :key="i"
          @click="changeActive(i)"
        >
          {{item.cat_name}}
        </view>
      </scroll-view>
      <!-- 右侧scroll -->
      <scroll-view class="cate-right" enable-flex="true" scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <view class="cate-right-item" v-for="(item, i) in cateLev2" :key="i">
          <!-- title -->
          <view class="cate-right-title">/{{item.cat_name}}/</view>
          <!-- 内容 -->
          <view class="cate-right-content">
            <view class="cate-right-box" v-for="(item2, i2) in item.children" @click="gotoGoodsList(item2)" :key="i2">
              <image class="cate-right-box-img" :src="item2.cat_icon" mode="widthFix"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import tabBarBadge from '@/mixin/tabBarBadge.js'
  
  export default {
    mixins: [tabBarBadge],
    data() {
      return {
        //页面状态数据
        active: 0, // 用户选中的分类项
        wh: 0, // 窗口可用高度,
        scrollTop: 10, // 滚动条距离滚动窗口顶部的距离
        
        
        // 显示数据
        cateList: [], // 分类数据
        cateLev2: [], // 二级分类数据
      };
    },
    onLoad() {
      // 获取屏幕可用高度
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight -50 // 适配搜索栏
      console.log('屏幕可用高度', this.wh)
      // 发起获取分类数据请求
      this.getCateList()
    },
    methods: {
      // 获取分类数据
      async getCateList(){
        const {data: res} = await uni.$http.get('/categories')
        console.log(res)
        // 判断数据异常
        if(res.meta.status !== 200) return uni.$showMsg()
        // 设置页面数据
        this.cateList = res.message
        this.cateLev2 = res.message[0].children // 二级分类数据
      },
      // 改变用户选中的一级索引
      changeActive(i){
        this.active = i
        // 更新二级分类数据
        this.cateLev2 = this.cateList[i].children
        // 滚动条到顶
        this.scrollTop = Math.random()
      },
      // 点击二级分类详情跳转到商品列表页
      gotoGoodsList(item){
        console.log(item)
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${item.cat_id}`
        })
      },
      // my-search 自定义组件事件处理函数
      searchClickHandler(e){
        console.log('my-search 组件被点击了', e)
        // 跳转到搜索页面
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }

  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;
    .cate-left{
      height: 800rpx;
      width: 200rpx;
      .cate-left-item {
        background-color: #efefef;
        height: 120rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 28rpx;
        &.active{
          background-color: #ffffff;
          display: flex;
          justify-content: space-between;
          align-items: center;
          &::before{
            content: '';
            width: 6rpx;
            height: 60rpx;
            background-color: #c00000;
          }
          &::after{
            content: '';
            width: 6rpx;
            height: 60rpx;
            background-color: #ffffff;
          }
        }
        
      }
    }
    
    .cate-right {
      // display: flex;
      flex: 1;
      .cate-right-item{
        .cate-right-title{
          display: flex;
          justify-content: center;
          align-items: center;
          font-weight: bold;
          margin: 16rpx;
          padding: 16rpx;
        }
        .cate-right-content{
          display: flex;
          flex-wrap: wrap;
          .cate-right-box {
            width: 33.33%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            box-sizing: border-box;
            padding: 16rpx;
            .cate-right-box-img {
              width: 100%;
            }
            text {
              font-size: 24rpx;
            }
          }
        }

      }
    }
    
  }
</style>
